package com.internetCafes.spms.web.sys.bizservice.impl;

import com.internetCafes.spms.web.sys.bizservice.MenuInfoBizService;
import com.internetCafes.spms.web.sys.model.MenuInfo;
import com.internetCafes.spms.web.sys.service.MenuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenuInfoBizServiceImpl implements MenuInfoBizService {

    @Autowired
    private MenuInfoService menuInfoService;

    @Override
    public List<MenuInfo> list() {
        List<MenuInfo> menuList = menuInfoService.list();
        Map<Long, List<MenuInfo>> menuInfoMapGroupById = menuList
                .stream()
                .collect(Collectors.groupingBy(MenuInfo::getMenuId));
        for (MenuInfo menuInfo : menuList) {
            if (menuInfoMapGroupById.containsKey(menuInfo.getParentId())) {
                List<MenuInfo> menuInfoParentList = menuInfoMapGroupById.get(menuInfo.getParentId());
                menuInfo.setParentName(menuInfoParentList.stream().findFirst().map(MenuInfo::getName).orElse(null));
            }
        }
        return menuList;
    }
}
